//
//  RCDiagramView.m
//  baseUIkitDemo
//
//  Created by kabu on 2017/9/12.
//  Copyright © 2017年 youedata. All rights reserved.
//

#import "RCDiagramView.h"

@implementation RCDiagramModel


@end


@interface RCDiagramView ()

@end
@implementation RCDiagramView

/**
 初始化
 
 @param frame 大小
 @param aFillColor 填充颜色
 @param aBackgroundColor 背景颜色
 @param aSpace 间距
 @return 实例
 */
- (instancetype) initWithFrame:(CGRect)frame fillColor:(UIColor*)aFillColor backgroundColor:(UIColor*)aBackgroundColor space:(CGFloat)aSpace{
    self=[super initWithFrame:frame];
    if (self) {
        self.fillColor=aFillColor;
        self.backgroundColor=aBackgroundColor;
        self.space=aSpace;
    }
    return self;
}


/**
 过滤数据
 */
-(NSArray*)filterSourceData{
    if ([self sourceIsArray]) {
        NSArray *sArr=self.sourceData;
        float max =[self filtterSouceDataForMaxValue];
        self.maxY=max;
        
        if (max<=0.0) {
            return [NSArray new];
        }
        NSMutableArray *drawMuArr=[[NSMutableArray alloc] initWithCapacity:1];
        for (NSInteger i=0; i<sArr.count; i++) {
            id itemValueY=sArr[i];
            RCDiagramModel *model=[self fitter:itemValueY idex:i];
            [drawMuArr addObject:model];
        }
        return [drawMuArr copy];
    }
    
    return [NSArray new];
}


/**
 循环处理每个数据获得绘制model
 
 @param itemValue 元数据每一条
 @param idx 条数
 @return 绘制单元实例
 */
- (RCDiagramModel*)fitter:(id)itemValue idex:(NSInteger)idx{
    RCDiagramModel *model=[RCDiagramModel new];
    return model;
}

/**
 过滤原始数据获取最大的绘制值
 
 @return 最大值
 */
- (float)filtterSouceDataForMaxValue{
    float max = 0.0;
    NSArray *sArr=self.sourceData;
    for (id item in sArr) {
        float itemValue=[item floatValue];
        if (itemValue>=max) {
            max=itemValue;
        }
    }
    max=max+max*TopMaxValueSpacePercent;
    return max;
}
/**
 是否是数组
 
 @return BOOL
 */
-(BOOL)sourceIsArray{
    if (self.sourceData) {
        if ([self.sourceData isKindOfClass:[NSArray class]]||[self.sourceData isKindOfClass:[NSMutableArray class]]) {
            NSArray *temp=self.sourceData;
            if (temp.count>0) {
                return YES;
            }
        }
    }
    return NO;
}
-(void)drawRect:(CGRect)rect{
    if (self.sourceData&&self.diagramModels) {
        [self painting];
    }
}
#pragma mark RCDiagram
-(void)painting{
    
}
- (void)dicgramSource:(id)aSourceData{
    _sourceData=aSourceData;
    NSArray *array= [self filterSourceData];
    _diagramModels=array;
}
@end
